#include "cal.h"

int
main(int argc, char **argv) {
        void *sym = malloc(8);
        int i, num, opt, lval, rval, o2n_sym;
        char *p;
        enum TYPE type;

        *expr_end = '\0';
        i = 0;
        while (i++ != argc-1) {
                strcat(expr_end, argv[i]);
                expr_end += strlen(argv[i]);
                if ((expr_end-expr) >= BUFSIZ)
                        err_exit("Stack Overflow");
        }

        p = expr;
        while (p < expr_end) {
                getsym(p, sym, &type);
                switch (type) {
                case NUM:
                        num = atoi((char *)sym);
                        PUSHN(num);
                        break;
                case OPT:
                        POPO(opt);
                        o2n_sym = O2N(sym);
                        if (opt!=-1 && CMPO(opt, o2n_sym) >= 0) {
                                PUSHO(o2n_sym);
                                POPN(rval);
                                POPN(lval);
                                PUSHN((f[opt])(lval, rval));
                        }else {
                                if (opt!=-1) PUSHO(opt);
                                PUSHO(o2n_sym);
                        }
                        break;
                case LPAR:
                        CALL;
                        break;
                case RPAR:
                        exce();
                        --o_p;  //Maybe RET would be better to understand
                        break;
                default:
                        err_exit("Unexpect Symbol");
                }
                p += strlen(sym);
        }

        exce();
        printf("The answer is %d\n", *n_stack);

        exit(EXIT_SUCCESS);
}